﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using Core.DAO;
using www.Core.Contract.DataContract;
using www.Data.BO;
using www.Models;

namespace www.Controllerss
{
    public class VideoController
    {
        #region value
        private readonly VideoRefBO _videoRefBo = new VideoRefBO();
        private readonly VideoValueBO _videoValueBo = new VideoValueBO();
        #endregion

        #region ham lay du lieu

        public IQueryable<RelaxVideoDataContract> GeVideoDataContracts()
        {
            using (var scope = new TransactionScope())
            {
                var lReturn = _videoRefBo.GetVideoDataContracts();
                scope.Complete();
                return lReturn;
            }
        }
      
        #endregion

        #region Add
        public long Add(List<RelaxVideoDataContract> videoDataContracts)
        {
            using (var scope = new TransactionScope())
            {
                var iD = 0;
                var story = new VIDEO_REF
                {
                    date = videoDataContracts[0].Date,
                    priority = videoDataContracts[0].Priority,
                    categoryId = Convert.ToInt32(videoDataContracts[0].CategoryId),
                    isdefault = videoDataContracts[0].IsDefault,
                    active = videoDataContracts[0].Active,
                    userId = videoDataContracts[0].Userid,
                    view = videoDataContracts[0].View,
                    userbrower = videoDataContracts[0].UserBrower
                };
                _videoRefBo.Add(story);
                var first = _videoRefBo.GetVideoDataContract().OrderByDescending(o => o.Id).FirstOrDefault();
                if (first != null)
                {
                    iD = Convert.ToInt32(first.Id);
                    foreach (var item in videoDataContracts)
                    {
                        var newValue = new VIDEO_VALUE
                        {
                            name = item.Name,
                            tag = item.Tag,
                            link = item.Code,
                            languagecode = item.Languagecode,
                            videoid = iD,
                            report = item.Report
                        };
                        _videoValueBo.Add(newValue);
                    }
                }
                scope.Complete();
                return iD;
            }
        }
        #endregion

        #region Update
        public Boolean Update(List<RelaxVideoDataContract> videoDataContracts)
        {
            using (var scope = new TransactionScope())
            {

                var story = new VIDEO_REF
                {
                    id = Convert.ToInt32(videoDataContracts[0].Id),
                    date = videoDataContracts[0].Date,
                    priority = videoDataContracts[0].Priority,
                    categoryId = Convert.ToInt32(videoDataContracts[0].CategoryId),
                    isdefault = videoDataContracts[0].IsDefault,
                    active = videoDataContracts[0].Active,
                    view = videoDataContracts[0].View,
                    userId = videoDataContracts[0].Userid,
                    userbrower = videoDataContracts[0].UserBrower
                };
                _videoRefBo.Update(story);
                foreach (var item in videoDataContracts)
                {
                    var newValue = new VIDEO_VALUE
                    {
                        videoid = Convert.ToInt32(item.Id),
                        languagecode = item.Languagecode,
                        name = item.Name,
                        tag = item.Tag,
                        link = item.Code,
                        report = item.Report
                    };
                    if (!_videoValueBo.Update(newValue))
                    {
                        _videoValueBo.Add(newValue);
                    }
                }
                scope.Complete();
                return true;
            }
        }
        public void UpdateView(long id)
        {
            using (var scope = new TransactionScope())
            {
                _videoRefBo.UpdateView(id);
                scope.Complete();
            }
           
        }
        #endregion

        #region Delete

        public Boolean Delete(List<long> lId, long iLanguageCode)
        {
            using (var scope = new TransactionScope())
            {
                foreach (var id in lId)
                {
                    _videoValueBo.Delete(id, iLanguageCode);
                }
                scope.Complete();
                return true;
            }
        }
        #endregion
    }
}